package com.yidu.ladder.delivery.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.base.domain.Area;
import com.yidu.ladder.delivery.dao.FreeDao;
import com.yidu.ladder.delivery.domain.CalculateFeeVo;
import com.yidu.ladder.delivery.domain.Free;
import com.yidu.ladder.delivery.service.FreeService;

/**
 * 运费业务逻辑接口实现类
 * 
 * @author yy
 * @since 2019-12-20
 *
 */
@Service
public class FreeServiceImpl implements FreeService {

	/**
	 * 关联运费数据访问层
	 */
	@Autowired
	private FreeDao freeDao;

	public FreeDao getFreeDao() {
		return freeDao;
	}

	public void setFreeDao(FreeDao freeDao) {
		this.freeDao = freeDao;
	}

	/**
	 * 查询所有的省
	 */
	@Override
	public List<Area> queryAllProvince() {
		//调用查询所有的省方法
		List<Area> areaList = freeDao.queryAllProvince();
		return areaList;
	}

	/**
	 * 查询某个省下所有市
	 */
	@Override
	public List<Area> queryAllCity(String province) {
		//调用查询某个省下所有市
		List<Area> areaList = freeDao.queryAllCity(province);
		return areaList;
	}

	/**
	 * 查询某个市下所有县
	 */
	@Override
	public List<Area> queryAllDistrict(String city) {
		//调用查询某个市下所有县
		List<Area> areaList = freeDao.queryAllDistrict(city);
		return areaList;
	}

	/**
	 * 计算运费
	 */
	@Override
	public Map<String, Object> calculateTheShipmentFree(CalculateFeeVo feeVo) {
		//定义BigDecimal
		BigDecimal fee = null;
		// -----------------------根据查询条件获得用于计算的费用数据----------------------------------------------------------------
		//定义一个map对象
		Map<String, Object> paramMap = new HashMap<>();
		//为map赋值开始省份
		paramMap.put("startProvince", feeVo.getStartProvince());

		// 开始省份 = 目的省份  同省
		if (feeVo.getStartProvince().equals(feeVo.getEndProvince())) {
			//开始城市 = 目的城市 同城
			if (feeVo.getStartCity().equals(feeVo.getEndCity())) { 
				//为map赋值区域同城
				paramMap.put("region", "同城");
			} else { 
				// 为map赋值区域同省
				paramMap.put("region", "同省");
			}
		} else { 
			//为map赋值区域跨省
			paramMap.put("region", "跨省");
			//为map赋值结束省份
			paramMap.put("endProvince", feeVo.getEndProvince());
		}
		// 执行计算运费方法
		Free free = freeDao.findFreeByCateria(paramMap);

		if (free == null) {
			// 抛出异常
			throw new RuntimeException("运费查询结果为空");
		}

		// ------------------------根据查询条件获得用于计算的费用数据---------------------------------------------------------------

		// ------------------------计算运费-------------------------------------------------------------------------------
		// 调用包装类的重量转换为double类型的重量包装类
		Double weightDouble = feeVo.getWeight().doubleValue();
		//重量<=1
		if (weightDouble <= 1) {
			// 首重
			fee = free.getHeadWeight();
		  //重量<=50
		} else if (weightDouble <= 50) {
			// 运费=（包装重量-1）*50 + 首重
			fee = feeVo.getWeight().subtract(BigDecimal.ONE).multiply(free.getT50()).add(free.getHeadWeight());
		  //重量<=100
		} else if (weightDouble <= 100) {
			// 运费=（包装重量-1）*100 + 首重
			fee = feeVo.getWeight().subtract(BigDecimal.ONE).multiply(free.getT100()).add(free.getHeadWeight());
			//重量<=300
		} else if (weightDouble <= 300) {
			// 运费=（包装重量-1）*300 + 首重
			fee = feeVo.getWeight().subtract(BigDecimal.ONE).multiply(free.getT300()).add(free.getHeadWeight());
		} else {
			// 运费=（包装重量-1）*>300 + 首重
			fee = feeVo.getWeight().subtract(BigDecimal.ONE).multiply(free.getGt300()).add(free.getHeadWeight());
		}

		// ------------------------计算到达时间-------------------------------------------------------------------------------
		//获取包装类的寄件时间
		Date postTimeDate = feeVo.getPostTime();
		//获取年，月， 日+当前时间
		Date arravDate = new Date(postTimeDate.getYear(), postTimeDate.getMonth(),
				postTimeDate.getDate() + free.getArrivalTime());

		// 将 fee和arravDate封装成Map,返回
		Map<String, Object> map = new HashMap<>();
		map.put("fee", fee);
		map.put("arravDate", arravDate);
		
		return map;
	}

}
